package dev.polls.skycube;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class GetJSon {
	JSONObject jo = new JSONObject();
	JSONObject grouplist = new JSONObject();
	JSONObject Messagelist = new JSONObject();
	JSONArray groupl = new JSONArray();
	JSONArray Message = new JSONArray();
	private String company_name;
	private String id;
	
	
	public JSONObject Json_Values(String usern, String passn) throws URISyntaxException, ClientProtocolException, IOException, JSONException {
		BufferedReader in = null;
		String output;
	   
	    	HttpClient client = new DefaultHttpClient(); 
	        HttpGet request = new HttpGet();	//define httpget 
	        request.setURI(new URI("http://www.skycube.eu/Android/Profile?Username="+usern+"&Password="+passn));
	        HttpResponse response = client.execute(request);
	        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
	        output=in.readLine();
	        System.out.println(output);
	        jo = new JSONObject(output); 
	  
		return jo;
	}
	public String getUserId(String username) throws URISyntaxException, ClientProtocolException, IOException
	{
		BufferedReader in = null;
		
		HttpClient client = new DefaultHttpClient(); 
        HttpGet request = new HttpGet();	//define httpget 
        request.setURI(new URI("http://www.skycube.eu/Android/GetUserId?Username="+username));
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        id =in.readLine();
		return id;
	}
	public JSONArray getCompany(String username, String password) throws URISyntaxException, ClientProtocolException, IOException, JSONException
	{
		BufferedReader in = null;
		
		HttpClient client = new DefaultHttpClient(); 
        HttpGet request = new HttpGet();	//define httpget 
        request.setURI(new URI("http://skycube.eu/android/company?username="+username+"&password="+password));
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        company_name =in.readLine();
        JSONArray companyInfo = new JSONArray(company_name);
        for (int i = 0; i < companyInfo.length(); ++i) {
            JSONObject rec = companyInfo.getJSONObject(i);
            String name = rec.getString("Name_n");
            System.out.println(name);
         }
        
		return companyInfo;
	}
	// static ingevoegd
	public JSONArray getGroupList(String username, String password) throws URISyntaxException, ClientProtocolException, IOException, JSONException
	{
		BufferedReader in = null;
		String out;
		
		HttpClient client = new DefaultHttpClient(); 
        HttpGet request = new HttpGet();	//define httpget 
        request.setURI(new URI("http://skycube.eu/Android/GroupList?Username="+username+"&Password="+password));
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        out =in.readLine();
        
        groupl = new JSONArray(out);
        System.out.println(out);
        for (int i = 0; i < groupl.length(); ++i) {
            JSONObject rec = groupl.getJSONObject(i);
            String name = rec.getString("Name");
            System.out.println(name);
         }
        
        return groupl;
	}
	
	public JSONArray getMessageList(String username, String password, int groupid) throws URISyntaxException, ClientProtocolException, IOException, JSONException
	{
		BufferedReader in = null;
		String out;
		
		HttpClient client = new DefaultHttpClient(); 
        HttpGet request = new HttpGet();	
        request.setURI(new URI("http://skycube.eu/Android/Messages?Username="+username+"&Password="+password+"&groupid="+groupid));
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        out =in.readLine();
        
        Message = new JSONArray(out);
        System.out.println(out);
        for (int i = 0; i < Message.length(); ++i) {
            JSONObject rec = Message.getJSONObject(i);
         }
        
        return Message;
	}
	

}


